#!/bin/bash

killall xlunch

TEMPCONF=/tmp/xlunch$$.conf
xlunch_genquick 64 --desktop > $TEMPCONF

COMMAND=$(
  xlunch --border 7% --sideborder 10% --borderratio 100 --sideborderratio 50 \
         --backgroundcolor 00000066 \
         --background /usr/share/wallpapers/slax_wallpaper.jpg --font DejaVuSans/11 \
         --button "/usr/share/icons/locolor/48x48/actions/xlunch-logout.png;/usr/share/icons/locolor/48x48/actions/xlunch-logout-hi.png;52;-2;fblogout" \
         --button "/usr/share/icons/hicolor/48x48/apps/org.xfce.panel.applicationsmenu.png;/usr/share/icons/hicolor/48x48/apps/org.xfce.panel.applicationsmenu-hi.png;2;-2;" \
         --button "/usr/share/icons/locolor/104x52/ui/slax-menu-bg.png;;0;-0;" \
         --voidclickterminate --iconpadding 40 --textpadding 10 \
         --leastmargin 6 --hidemissing --iconsize 64 \
         --highlight /usr/share/icons/hicolor/128x128/apps/xlunch_highlight.png \
         --outputonly --input $TEMPCONF
)

rm $TEMPCONF

if [ "$COMMAND" = "" ]; then
   exit
fi

if [ "$COMMAND" = "fblogout" ]; then
   fblogout
   exit
fi

fbstartupnotify

# if command is a .desktop file, parse it
if [[ "$COMMAND" =~ \.desktop ]]; then
   NoTerm="$(cat "$COMMAND" | grep Terminal\\s*= | grep -i "Terminal\\s*=\\s*false")"
   COMMAND="$(cat "$COMMAND" | grep Exec\\s*= | head -n 1 | sed -r s/.*=// | sed -r "s/%[^%]+//g")"
fi

cmd="$(echo $COMMAND | sed -r "s/\\s.*//")"
whi="$(which $cmd | head -n 1)"
Xdep=$(ldd $whi | grep libX11)
Ndep=$(ldd $whi | grep libncurses)

WAIT='echo "--------------------------------------------------"; read -n 1 -s -r -p "Command finished. Press any key to close window..."'
if [ "$Ndep" != "" -o "$cmd" = "man" -o "$cmd" = "mc" ]; then
   WAIT=""
fi

if [ "$Xdep" = "" -a "$cmd" != "chromium" -a "$cmd" != "wicd-manager" -a "$cmd" != "fbliveapp" -a "$NoTerm" = "" ]; then
   exec xterm -ls -e bash --login -c -- "echo $USER@$HOSTNAME:$PWD# '$COMMAND'; $COMMAND; $WAIT"
else
   exec $COMMAND
fi
